package com.hs.rac.form;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import javax.swing.JComponent;
import javax.swing.JToggleButton;

import com.teamcenter.rac.classification.common.KeyLOVPopupButton;
import com.teamcenter.rac.common.lov.HierarchicalLOVComponent;
import com.teamcenter.rac.common.lov.LOVComboBox;
import com.teamcenter.rac.common.lov.LOVPopupButton;
import com.teamcenter.rac.common.lov.LOVUIComponent;
import com.teamcenter.rac.kernel.TCComponentListOfValues;
import com.teamcenter.rac.kernel.TCSession;

public class CustomLOVUIComponent extends LOVUIComponent 
{
    private JComponent lovUIComp;

	public CustomLOVUIComponent()
    {
        super(null);
    }

    public CustomLOVUIComponent(TCComponentListOfValues imancomponentlistofvalues, String s, int i)
    {
    	 super(imancomponentlistofvalues, s, i, null, ((Object []) (null)));
    }

    public CustomLOVUIComponent(TCComponentListOfValues imancomponentlistofvalues, String s, int i, Object obj, Object aobj[])
    {
   	 super(imancomponentlistofvalues, s, i, false, obj, aobj);
    }

    public CustomLOVUIComponent(TCComponentListOfValues imancomponentlistofvalues, String s, int i, boolean flag)
    {
    	 super(imancomponentlistofvalues, s, i , flag);
    }

    public CustomLOVUIComponent(TCComponentListOfValues imancomponentlistofvalues, String s)
    {
        this(imancomponentlistofvalues, s, -1);
    }

    public CustomLOVUIComponent(TCComponentListOfValues imancomponentlistofvalues)
    {
        this(imancomponentlistofvalues, null);
    }

    public CustomLOVUIComponent(TCSession imansession, String s, String s1)
    {
        this(imansession, s, s1, -1);
    }

    public CustomLOVUIComponent(TCSession imansession, String s)
    {
        this(imansession, s, -1);
    }

    public CustomLOVUIComponent(TCSession imansession, String s, String s1, int i)
    {
        this(imansession, s, s1, i, false);
    }

    public CustomLOVUIComponent(TCSession imansession, String s, String s1, int i, boolean flag, Object obj, Object aobj[])
    {
   	 super(imansession, s, s1, i, flag, obj, aobj);
    }

    public CustomLOVUIComponent(TCComponentListOfValues imancomponentlistofvalues, String s, int i, boolean flag, Object obj, Object aobj[])
    {
   	 super(imancomponentlistofvalues,s, i, flag, obj, aobj);
    }

    public CustomLOVUIComponent(TCSession imansession, String s, String s1, int i, boolean flag)
    {
        this(imansession, s, s1, i, flag, null, null);
    }

    public CustomLOVUIComponent(TCSession imansession, String s, int i)
    {
        this(imansession, s, ((String) (null)), i);
    }
    
    /**
     * Overwrite this function to add ItemChangeListener to catch value change event
     * @param	imancomponentlistofvalues	listofvalues
     * @param	s	initialize value
     */
	 public void setLovComponent(TCComponentListOfValues imancomponentlistofvalues, String s)
    {
		 lovChange = new PropertyChangeSupport(this);
		 super.setLovComponent(imancomponentlistofvalues, s);
		 
		// System.out.println("LOVChange component type " +lovUIComp.getClass());
		 
		 if(lovUIComp instanceof LOVPopupButton)
		 {
			 ((LOVPopupButton)lovUIComp).addItemListener(new ItemListener()
			 {
				 public void itemStateChanged(ItemEvent arg0)
				 {
					 grpBoardValueChange((JComponent)arg0.getSource());
				 }
			 });
		 }
		 else if(lovUIComp instanceof LOVComboBox)
		 {
			 ((LOVComboBox)lovUIComp).addActionListener(new ActionListener()
			 {
				 public void actionPerformed(ActionEvent arg0)
				 {
					 grpBoardValueChange((JComponent)arg0.getSource());
				 }
			 });

		 }
		 else if(lovUIComp instanceof HierarchicalLOVComponent)
		 {
			 ((HierarchicalLOVComponent)lovUIComp).addActionListener(new ActionListener()
			 {
				 public void actionPerformed(ActionEvent arg0)
				 {
					 grpBoardValueChange((JComponent)arg0.getSource());
				 }
			 });
		 }
		 else if(lovUIComp instanceof KeyLOVPopupButton)
		 {
			 ((KeyLOVPopupButton)lovUIComp).addItemListener(new ItemListener()
			 {
				 public void itemStateChanged(ItemEvent arg0) 
				 {
					 grpBoardValueChange((JComponent)arg0.getSource());
				 }
			 });
		 }
    }
	 
	 /**
	  * set new value
	  * @param comp
	  */
	 private void grpBoardValueChange(JComponent comp)
	 {
		 String s = null;
        if(comp instanceof JToggleButton)
        {
            JToggleButton jtogglebutton = (JToggleButton)comp;
            s = jtogglebutton.getText();
        }
        if(comp instanceof LOVComboBox)
        {
           //s = ((LOVComboBox)comp).getSelectedString();
       	 s = ((LOVComboBox)comp).getSelectedItem().toString();
        }
        if(comp instanceof HierarchicalLOVComponent)
        {
           //s = ((HierarchicalLOVComponent)comp).getSelectedString();
       	 s = ((HierarchicalLOVComponent)comp).getSelectedItem().toString();
        }
        if(comp instanceof KeyLOVPopupButton)
            s = ((KeyLOVPopupButton)comp).getSelectedItem().toString();
   
        
        selectValueChange(s);
	 }
	 
	 /**
	  * JComponent used in LOVUIComponent
	  * @return
	  */
	 public JComponent getComponent()
	 {
		 return lovUIComp;
	 }
	 
	 /**
	  * selectValue 
	  */
	 private String selectValue = null;
	 
	 /**
	  * store property change listeners
	  */
	 private PropertyChangeSupport lovChange;
	 
	 /**
	  * add PropertyChangeListener
	  * @param l	PropertyChangeListener
	  */
	 public void addLOVValueChangeListener(PropertyChangeListener l)
	 {
		 lovChange.addPropertyChangeListener(l);
	 }
	 
	 /**
	  * remove PropertyChangeListener
	  * @param l	PropertyChangeListener
	  */
	 public void removeLOVValueChangeListener(PropertyChangeListener l)
	 {
		 lovChange.removePropertyChangeListener(l);
	 }
	 
	 /**
	  * create a propertychange event when set a new value, and notify all propertychange listeners
	  * @param s value
	  */
	 public void selectValueChange(String s)
	 {		 

		 PropertyChangeEvent propertychangeevent = new PropertyChangeEvent(this, "LOVValue", selectValue, s);
		 selectValue = s;
		 lovChange.firePropertyChange(propertychangeevent);
	 }
}
